{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64\n",
    "import json\n",
    "import os\n",
    "import pickle\n",
    "import zlib\n",
    "from datetime import datetime\n",
    "\n",
    "from datasets import load_dataset\n",
    "\n",
    "livecodebench = load_dataset(\"livecodebench/code_generation_lite\", version_tag=\"release_v5\", trust_remote_code=True, split=\"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pprint import pprint\n",
    "\n",
    "pprint(livecodebench[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# deepseek paper uses lcb from 2024-08 to 2025-01\n",
    "def is_date_in_range_for_test(date_str):\n",
    "    date_obj = datetime.strptime(date_str, \"%Y-%m-%dT%H:%M:%S\")\n",
    "    # Define the start and end range\n",
    "    start_date = datetime(2024, 8, 1)  # August 1, 2024\n",
    "    end_date = datetime(2025, 2, 1)  # February 1, 2025\n",
    "\n",
    "    # Check if the date is within the range\n",
    "    return start_date <= date_obj < end_date\n",
    "\n",
    "\n",
    "dataset = []\n",
    "for entry in livecodebench:\n",
    "    tests = entry[\"public_test_cases\"]\n",
    "    if isinstance(tests, str):\n",
    "        try:\n",
    "            tests = json.loads(tests)\n",
    "        except json.JSONDecodeError as e:\n",
    "            print(f\"code reward Json Error parsing livecodebench: {e}\")\n",
    "            continue\n",
    "    private_tests = pickle.loads(\n",
    "        zlib.decompress(\n",
    "            base64.b64decode(entry[\"private_test_cases\"].encode(\"utf-8\"))  # type: ignore\n",
    "        )\n",
    "    )\n",
    "    if isinstance(private_tests, str):\n",
    "        try:\n",
    "            private_tests = json.loads(private_tests)\n",
    "        except json.JSONDecodeError as e:\n",
    "            print(f\"code reward Json Error parsing livecodebench: {e}\")\n",
    "            continue\n",
    "    assert isinstance(private_tests, list)\n",
    "    tests.extend(private_tests)\n",
    "\n",
    "    if len(tests) == 0:\n",
    "        continue\n",
    "\n",
    "    for input in tests:\n",
    "        assert isinstance(input[\"input\"], str)\n",
    "        assert isinstance(input[\"output\"], str)\n",
    "\n",
    "    metadata = json.loads(entry[\"metadata\"])\n",
    "    if tests[0][\"testtype\"] == \"functional\":\n",
    "        assert metadata, f\"Metadata is not found, check if your LCB data is preprocessed correctly: {entry}\"\n",
    "    new_entry = {\n",
    "        \"problem\": entry[\"question_content\"],\n",
    "        \"starter_code\": entry[\"starter_code\"],\n",
    "        \"tests\": tests,\n",
    "        \"metadata\": metadata,\n",
    "    }\n",
    "    if is_date_in_range_for_test(entry[\"contest_date\"]):\n",
    "        dataset.append(new_entry)\n",
    "\n",
    "print(f\"Dataset size: {len(dataset)}\")\n",
    "\n",
    "output_dir = os.path.abspath(\"../../test/code\")\n",
    "output_file = os.path.join(output_dir, \"livecodebench.json\")\n",
    "\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# deepseek paper uses lcb from 2024-08 to 2025-01\n",
    "def is_date_in_range_for_train(date_str):\n",
    "    date_obj = datetime.strptime(date_str, \"%Y-%m-%dT%H:%M:%S\")\n",
    "    # Define the start and end range\n",
    "    start_date = datetime(2023, 5, 1)  # May 1, 2023\n",
    "    end_date = datetime(2024, 8, 1)  # August 1, 2024\n",
    "\n",
    "    # Check if the date is within the range\n",
    "    return start_date <= date_obj < end_date\n",
    "\n",
    "\n",
    "dataset = []\n",
    "for entry in livecodebench:\n",
    "    tests = entry[\"public_test_cases\"]\n",
    "    if isinstance(tests, str):\n",
    "        try:\n",
    "            tests = json.loads(tests)\n",
    "        except json.JSONDecodeError as e:\n",
    "            print(f\"code reward Json Error parsing livecodebench: {e}\")\n",
    "            continue\n",
    "\n",
    "    private_tests = pickle.loads(\n",
    "        zlib.decompress(\n",
    "            base64.b64decode(entry[\"private_test_cases\"].encode(\"utf-8\"))  # type: ignore\n",
    "        )\n",
    "    )\n",
    "    if isinstance(private_tests, str):\n",
    "        try:\n",
    "            private_tests = json.loads(private_tests)\n",
    "        except json.JSONDecodeError as e:\n",
    "            print(f\"code reward Json Error parsing livecodebench: {e}\")\n",
    "            continue\n",
    "    assert isinstance(private_tests, list)\n",
    "    tests.extend(private_tests)\n",
    "\n",
    "    for input in tests:\n",
    "        assert isinstance(input[\"input\"], str)\n",
    "        assert isinstance(input[\"output\"], str)\n",
    "\n",
    "    if len(tests) <= 4:\n",
    "        continue\n",
    "    metadata = json.loads(entry[\"metadata\"])\n",
    "    if tests[0][\"testtype\"] == \"functional\":\n",
    "        assert metadata, f\"Metadata is not found, check if your LCB data is preprocessed correctly: {entry}\"\n",
    "    new_entry = {\n",
    "        \"problem\": entry[\"question_content\"],\n",
    "        \"starter_code\": entry[\"starter_code\"],\n",
    "        \"tests\": tests,\n",
    "        \"metadata\": metadata,\n",
    "    }\n",
    "    if is_date_in_range_for_train(entry[\"contest_date\"]):\n",
    "        dataset.append(new_entry)\n",
    "\n",
    "print(f\"Dataset size: {len(dataset)}\")\n",
    "\n",
    "output_dir = os.path.abspath(\"../../train/code\")\n",
    "output_file = os.path.join(output_dir, \"livecodebench.json\")\n",
    "\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rllm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
